package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.li.youhappygo.mapper.CustomerMapper;
import com.li.youhappygo.mapper.ProductMapper;
import com.li.youhappygo.mapper.UserBrowseHistoryMapper;
import com.li.youhappygo.mapper.UserFavoritesMapper;
import com.li.youhappygo.model.entity.product.Product;
import com.li.youhappygo.model.entity.user.UserBrowseHistory;
import com.li.youhappygo.model.entity.user.UserFavorites;
import com.li.youhappygo.model.entity.user.Users;
import com.li.youhappygo.model.vo.User.UserBrowseHistoryVo;
import com.li.youhappygo.model.vo.customer.CustomerDetailVo;
import com.li.youhappygo.model.vo.customer.CustomerVo;
import com.li.youhappygo.service.CustomerService;
import com.li.youhappygo.service.ProductService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    UserBrowseHistoryMapper userBrowseHistoryMapper;

    @Autowired
    UserFavoritesMapper userFavoritesMapper;

    @Autowired
    ProductMapper productMapper;
    @Override
    public List<CustomerVo> getAllCustomer(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CustomerVo> customerVos= new ArrayList<>();
        List<Users> users = customerMapper.selectList(null);
        for (Users user:users) {
            CustomerVo customerVo = new CustomerVo();
            //userFavorites
            customerVo.setUserId(user.getId());
            BeanUtils.copyProperties(user,customerVo);
//            QueryWrapper<UserFavorites> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.eq("user_id",user.getId());
//            List<UserFavorites> userFavoritesList = userFavoritesMapper.selectList(queryWrapper1);
//            customerVo.setUserFavoritesList(userFavoritesList);
//            //userBrowseHistories
//            List<UserBrowseHistoryVo> userBrowseHistoryVoList = new ArrayList<>();
//
//            QueryWrapper<UserBrowseHistory> queryWrapper2 = new QueryWrapper<>();
//            queryWrapper2.eq("user_id",user.getId());
//            List<UserBrowseHistory> userBrowseHistoryList = userBrowseHistoryMapper.selectList(queryWrapper2);
//            for (UserBrowseHistory userBrowseHistory:userBrowseHistoryList){
//                UserBrowseHistoryVo userBrowseHistoryVo = new UserBrowseHistoryVo();
//                userBrowseHistoryVo.setId(userBrowseHistory.getId());
//                List<Product> products = productMapper.selectList(new QueryWrapper<Product>().eq("id", userBrowseHistory.getProductId()));
//                userBrowseHistoryVo.setProductList(products);
//                userBrowseHistoryVoList.add(userBrowseHistoryVo);
//            }
//
//            customerVo.setUserBrowseHistories(userBrowseHistoryVoList);
            customerVos.add(customerVo);
        }

        return customerVos;
    }

    @Override
    public CustomerDetailVo getCustomerInfoById(Integer userId) {
        CustomerDetailVo customerVo = new CustomerDetailVo();
        List<Users> users = customerMapper.selectList(null);
        Users user = customerMapper.selectOne(new QueryWrapper<Users>().eq("id", userId));
        //userFavorites
//        customerVo.setUserId(user.getId());
        BeanUtils.copyProperties(user,customerVo);
        QueryWrapper<UserFavorites> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",user.getId());
        List<UserFavorites> userFavoritesList = userFavoritesMapper.selectList(queryWrapper1);
        customerVo.setUserFavoritesList(userFavoritesList);
        //userBrowseHistories
        List<UserBrowseHistoryVo> userBrowseHistoryVoList = new ArrayList<>();

        QueryWrapper<UserBrowseHistory> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id",user.getId());
        List<UserBrowseHistory> userBrowseHistoryList = userBrowseHistoryMapper.selectList(queryWrapper2);
        for (UserBrowseHistory userBrowseHistory:userBrowseHistoryList){
            UserBrowseHistoryVo userBrowseHistoryVo = new UserBrowseHistoryVo();
            userBrowseHistoryVo.setId(userBrowseHistory.getId());
            List<Product> products = productMapper.selectList(new QueryWrapper<Product>().eq("id", userBrowseHistory.getProductId()));
            userBrowseHistoryVo.setProductList(products);
            userBrowseHistoryVoList.add(userBrowseHistoryVo);
        }

        customerVo.setUserBrowseHistories(userBrowseHistoryVoList);
        return customerVo;
    }
}
